package com.sunday.volunteer.utils;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.util.Log;

import com.sunday.common.widgets.UIAlertView;
import com.sunday.member.base.BaseFragment;


/**
 * Created by Pual's PC on 2016/9/19.
 */
public class CallFragment extends BaseFragment {

    private String mobile;
    protected void toCall(String mobile){
        this.mobile = mobile;
        PackageManager pkgManager = getActivity().getPackageManager();

        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getActivity().getPackageName()) == PackageManager.PERMISSION_GRANTED;
        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, getActivity().getPackageName()) == PackageManager.PERMISSION_GRANTED;
        boolean isCall = pkgManager.checkPermission(Manifest.permission.CALL_PHONE, getActivity().getPackageName()) == PackageManager.PERMISSION_GRANTED;
        boolean isMms = pkgManager.checkPermission( Manifest.permission.RECEIVE_MMS ,  getActivity().getPackageName()) == PackageManager.PERMISSION_GRANTED;

        Log.d("iiii" , pkgManager.checkPermission( Manifest.permission.RECEIVE_MMS ,  getActivity().getPackageName()) +"---"+pkgManager.checkPermission(Manifest.permission.CALL_PHONE, getActivity().getPackageName()));
        if (Build.VERSION.SDK_INT >= 23 && !isCall) {
            requestPermissions(new String[] { Manifest.permission.CALL_PHONE },0);
        } else {
            // SDK初始化，第三方程序启动时，都要进行SDK初始化工作
            //PushManager.getInstance().initialize(this.getApplicationContext());
            Call();
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
            // SDK初始化，第三方程序启动时，都要进行SDK初始化工作
            //PushManager.getInstance().initialize(this.getApplicationContext());
            Call();
        } else {
            //checkMMS();
            show();
        }
    }

    public void Call(){
        /*TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
        if(TextUtils.isEmpty(tm.getSimSerialNumber())){
            ToastUtils.showToast(mContext , "请先安装SIM卡");
            return ;
        }*/
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(String.format("tel:%s" , mobile)));
        startActivity(intent);
    }

    private void show(){
        final UIAlertView view = new UIAlertView(mContext , "温馨提示" , "权限已被关闭，是否需要重新设置" , "取消" , "确定");
        view.setClicklistener(new UIAlertView.ClickListenerInterface() {
            @Override
            public void doLeft() {
                view.dismiss();
            }

            @Override
            public void doRight() {
                view.dismiss();
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }
        });
        view.show();
    }

}
